/*
** Created by Gahon Alexis
*/

#include "NetworkManager.hh"


sf::Packet& operator <<(sf::Packet& Packet, const NetPacket& C)
{
    Packet
        << C._header._slotType
        << C._header._packetId
        << C._header._len
        << C.data;
    return Packet;
}

NetworkManager::NetworkManager(bool connected) : Connected(connected), ipServer("10.18.206.238")
{
}

NetworkManager::~NetworkManager()
{
    //dtor
}


void    NetworkManager::RunTCP()
{
    sf::SelectorTCP Selector;
    sf::Packet      Packet;

    this->TCPSend();
    this->TCPReceive();

    while(true)
    {
        unsigned int NbSockets = Selector.Wait();
         // We can read from each returned socket
        for (unsigned int i = 0; i < NbSockets; ++i)
        {
            std::cout << "ici" << std::endl;
            // Get the current socket
            sf::SocketTCP Socket = Selector.GetSocketReady(i);
            if (Socket.Receive(Packet) == sf::Socket::Done)
            {
                // Extract the message and display it
                std::string Message;
                Packet >> Message;
                std::cout << "A client says : \"" << Message << "\"" << std::endl;
            }
            else
                Selector.Remove(Socket);
        }
    }
}

bool    NetworkManager::TCPConnect()
{
    if (this->Listener.Connect(4243, this->ipServer) != sf::Socket::Done)
    {
        std::cout << "FAIL CONNECT\n";
        return false;
    }
    std::cout << "SUCCESS CONNECT\n";
    return true;
}

bool    NetworkManager::TCPSend()
{
    NetPacket   Packet;

    Packet._header._slotType = 3;
    Packet._header._packetId = 0;
    Packet._header._len      = strlen("marino");
    strcpy(Packet.data, "marino");

    this->Listener.Send(reinterpret_cast<char*>(&Packet), sizeof(Packet));
    return false;
}

bool    NetworkManager::TCPReceive()
{
    NetPacket   Packet;
    unsigned int         nbRead;

    std::cout << "ICI" << std::endl;
    this->Listener.Receive(reinterpret_cast<char*>(&Packet), sizeof(NetPacket), nbRead);
    std::cout << "ICI2" << std::endl;

    std::cout << Packet._header._slotType << Packet._header._packetId << Packet._header._len << std::endl;

    return false;
}

bool    NetworkManager::TCPDisconnect()
{
    this->Listener.Close();
}

void    NetworkManager::RunUPD()
{
   /* sf::SelectorUDP Selector;

    while(true)
    {
        unsigned int NbSockets = Selector.Wait();
         // We can read from each returned socket
        for (unsigned int i = 0; i < NbSockets; ++i)
        {
            // Get the current socket
            sf::SocketUDP Socket = Selector.GetSocketReady(i);
            sf::Packet      Packet;
            if (Socket.Receive(Packet) == sf::Socket::Done)
            {
                // Extract the message and display it
                std::string Message;
                Packet >> Message;
                std::cout << "A client says : \"" << Message << "\"" << std::endl;
            }
            else
                Selector.Remove(Socket);
    }*/
}

void    NetworkManager::UPDBind()
{

}

void    NetworkManager::UDPSend()
{

}

void    NetworkManager::UDPReceive()
{

}
